// pages/recycle/recycle.js
import api from '../../config/api'
import { shareOptions } from '../../utils/access'
import request from '../../utils/request'
import { jump, stringify } from "../../utils/utils"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryList: [], //物品列表
    selectedList: [], //选择的物品列表
    sumWeight: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getThing()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
    * 获取所有物品
    */
    async getThing() {
      let res = await request.get(api.getRecycleGoodsList,{},{ loading: false })
      console.log('物品：', res)

      if (res.httpStatus == 200) {
        res.data.map((item) => {
          item.value = 1
          item.selected = false
        })

        this.setData({
          categoryList: res.data
        })
      }
    },
    /**
     * 选择种类
     */
    handleSelected(e) {
      const { categoryList } = this.data;
      const { dataset = {} } = e.currentTarget;
      const list = categoryList.map(item => (
        {
          ...item,
          selected: item.id === dataset.id ? !item.selected : item.selected,
          value: item.id === dataset.id ? 1 : item.value,
        }
      ));
      const selectedList = list.filter(item => item.selected);

      this.getSumWeight(selectedList);

      this.setData({
        categoryList: list,
        selectedList,
      })
    },
    /**
     * 清空已选择的物品
     */
    handleEmpty() {
      const { categoryList } = this.data;

      const list = categoryList.map(item => (
        {
          ...item,
          selected: false,
          value: 1,
        }
      ))

      this.setData({
        categoryList: list,
        selectedList: [],
        sumWeight: 0
      })
    },
    /**
     * 减少/添加所选物品的数量
     */
    handleChange(e) {
      const { categoryList, selectedList } = this.data;
      const { dataset: { item = {}, option } } = e.currentTarget;

      if((option === 'subtraction' && item.value === 1) || (option === 'plus' && item.value >=100)){
        return
      }

      const cList = this.handleChangeNumber(categoryList, option, item);
      const sList = this.handleChangeNumber(selectedList, option, item);

      this.getSumWeight(sList);

      this.setData({
        categoryList: cList,
        selectedList: sList,
      })
    },
    /**
     * 选中物品的数量变化
     */
    handleChangeNumber(list, option, item) {
      const data = list.map(sl => (
        {
          ...sl,
          value: sl.id === item.id ? (option === 'plus' ? (sl.value + 1) : (sl.value - 1)) : sl.value
        }
      ))
      return data
    },
    /**
     * 计算总计重量
     */
    getSumWeight(list) {
      let sumWeight = 0
      const { categoryList } = this.data
      list.map(item => {
        categoryList.map(sc => {
          if (item.id === sc.id) {
            sumWeight += item.value
          }
          return sc
        })
        return item
      })

      this.setData({
        sumWeight
      })
    },
    /**
     * 下一步
     */
    goNext() {
      const { selectedList, sumWeight } = this.data;
      if (selectedList.length <= 0) {
        return
      }
      jump('recycleFurnitureInfo', {
        obj: stringify({
          selectedList,
          sumPrice: sumWeight,
          type: 'recycle'
        })
      })
    },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    return shareOptions('default', e)
  }
})